﻿using System;
using System.IO;
using log4net;
using log4net.Core;

namespace CSharpExt.Logging
{
    public static class Logger
    {
        #region Factory Methods
        
        public static void Configure()
        {
            Configure("logging.xml");
        }

        public static void Configure(String filePath)
        {
            if(String.IsNullOrEmpty(filePath))
            {
                log4net.Config.BasicConfigurator.Configure();
                return;
            }
            
            FileInfo fi = new FileInfo(filePath);
            if(fi.Exists)
            {
                log4net.Config.XmlConfigurator.ConfigureAndWatch(fi);
                LogManager.GetLogger("Logging").FatalFormat("Service Logging initialized using '{0}'", fi.FullName);
            }
            else
            {
                log4net.Config.BasicConfigurator.Configure();
                Logger.GetLogger("Logging").FatalFormat("Unable to initialize logger with file {0}", fi.FullName);
            }
        }

        public static ILog GetLogger()
        {
            return LogManager.GetLogger("");
        }

        public static ILog GetLogger(Type type)
        {
            return LogManager.GetLogger(type);
        }

        public static ILog GetLogger(String name)
        {
            return LogManager.GetLogger(name);
        }
        
        public static ILog GetLogget(String format, params Object[] args)
        {
            String name = String.Format(format, args);
            return GetLogger(name);
        }

        #endregion
    }

}
